# coding=utf-8
import urllib2

from rpialert.base.beanret import BeanRet
from rpialert.base.cachedata import CacheData
from rpialert.base.log4py import logger
from rpialert.base.tools.cachedataclient import CacheDataClient
from rpialert.service.basesv import BaseSV

"""
测试记录业务接口
"""


class TestRecordSV(BaseSV):
    def buildTestRecord(self, jobnumber="", testOptionCode="", state=""):
        '''
        创建测试记录信息
        1.组装信息
        2.提交信息
        :return:
        '''

        # 1.组装信息
        sn = CacheData().toObj(CacheDataClient().read()).getSN
        data = 'jobNumber={jobnumber}&sn={sn}&testOptionCode={testOptionCode}&state={state}'
        data = data.replace('{jobnumber}', jobnumber).replace('{sn}', sn).replace(
            '{testOptionCode}', testOptionCode).replace('{state}', state)

        # 2.提交信息
        url = self.host + '/testRecord/build'
        logger.info('创建测试记录信息 ===>' + str(url))
        logger.info('创建测试记录信息 ===>' + str(data))
        req = urllib2.Request(url, data)
        response = urllib2.urlopen(req)
        result = response.read()
        logger.info('创建测试记录信息<===' + result)
        beanret = BeanRet().toObj(result)
        return beanret
